<?php 
class Request_a_quote_model extends MY_Model{
	
	function Request_a_quote_model(){
        parent::MY_Model();
		//$this->setTableName('network');
		$this->quotes_table = 'bur_quotes';
        $this->bur_papercolours='bur_papercolours';
	}
	function requestaquote_insert($data_quote = array()){
		
		//$this->load->database();
		/*$data_quote=array("quote_fullname"=>$fullname,
					"quote_companyname"=>$companyname,	
					"quote_telephone"=>$telephone,	
					"quote_email"=>$email,	
					"quote_jobtitle"=>$jobtitle,	
					"quote_papercolourid"=>$papercolour_id,	
					"quote_papersizeid"=>$papersize_id,
                    "quote_paperwidth"=>$papersize_width,	
                    "quote_paperheight"=>$papersize_height,	
					"quote_papername"=>$papername_id,	
					"quote_quantity"=>$quantity,	
					"quote_finishing"=>$finishing,	
					"quote_artwork"=>$artwork,
                    "quote_artwork_size"=>$artwork_size,
                    "quote_date"=>date('Y-m-d H:i:s')
					
					);*/
	
	
   				$query=$this->db->insert('bur_quotes',$data_quote);
  
    
    
		}
        
        function send_request_a_quote_email_admin($fullname,$companyname,$telephone,$email,$jobtitle,$papercolour_id
          ,$papersize_id='',$papersize_width='',$papersize_height='',$papername_id='',$quantity,$finishing,$artwork='',$artwork_size='')
    {
    	$config = array(
    		'mailtype'=>'html'
        );
         
      $data =   array("user"=>'admin',
        "quote_fullname"=>$fullname,
					"quote_companyname"=>$companyname,	
					"quote_telephone"=>$telephone,	
					"quote_email"=>$email,	
					"quote_jobtitle"=>$jobtitle,	
					"quote_papercolourid"=>$papercolour_id,	
					"quote_papersizeid"=>$papersize_id,
                    "quote_paperwidth"=>$papersize_width,	
                    "quote_paperheight"=>$papersize_height,	
					"quote_papername"=>$papername_id,	
					"quote_quantity"=>$quantity,	
					"quote_finishing"=>$finishing,	
					"quote_artwork"=>$artwork,
                    "quote_artwork_size"=>$artwork_size
					
					);
        
        $email_content = $this->load->view(TEMPLATE_NAME.'/front/mail/request_a_quote',$data, true);
		//print_r($email_content);die();
        
        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->from('noreply@mykprisguide.com', NOREPLY_ADD);
       
         $this->email->to($this->requestaquote_admin_email('bur_admin_emails'));
      
        $this->email->subject('New quote request');
        $this->email->message($email_content);                
      $this->email->send();
        return true;	
  }
  function send_request_a_quote_email_client($fullname,$companyname,$telephone,$email,$jobtitle,$papercolour_id
          ,$papersize_id,$papersize_width='',$papersize_height='',$papername_id='',$quantity,$finishing,$artwork='',$artwork_size='')
    {
    	$config = array(
    		'mailtype'=>'html'
        );
        
       
      
      $data =   array("user"=>'client',
       "quote_fullname"=>$fullname,
					"quote_companyname"=>$companyname,	
					"quote_telephone"=>$telephone,	
					"quote_email"=>$email,	
					"quote_jobtitle"=>$jobtitle,	
					"quote_papercolourid"=>$papercolour_id,	
					"quote_papersizeid"=>$papersize_id,
                    "quote_paperwidth"=>$papersize_width,	
                    "quote_paperheight"=>$papersize_height,	
					"quote_papername"=>$papername_id,	
					"quote_quantity"=>$quantity,	
					"quote_finishing"=>$finishing,	
					"quote_artwork"=>$artwork,
                     "quote_artwork_size"=>$artwork_size
                    
					
					);
        
        $email_content = $this->load->view(TEMPLATE_NAME.'/front/mail/request_a_quote',$data, true);
        
        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->from('info@mykprisguide.com', NOREPLY_ADD);
        
             $this->email->to($this->input->post('email')); 
             // $this->input->post('email');
            
        
      
        
        //$this->email->to('agoes.doank@gmail.com', 'general@icreativelabs.com', 'mailbox@icreativelabs.com' );
        //$this->email->reply_to('donations@turnitaround.co.za', 'Turn It Around');
        //$this->email->to('info@turnitaround.co.za');       
        $this->email->subject('New quote request ');
        $this->email->message($email_content);                
       $this->email->send();
        return true;	
  }
        function requestaquote_admin_email($table_name){
           $query=$this->db->get($table_name);
            if($query->num_rows()>0){
                $admin_email =$query->result_array();
                 foreach($admin_email as $key=>$email):
               
                if(!$email['email_address']=='')
                { $admin_emails[]= $email['email_address'];}
                endforeach;
                return $admin_emails;
            } 
            
        }
        
        function get_data_colour($id){
            $this->tableName='bur_papercolours';
            $where='papercolour_id='.$id;
            $result=$this->getDetail('',$where);
      
            return $result['papercolour_title'];
           
            
            
        }
        function get_data_papersize($id){
            $this->tableName='bur_papersizes';
            $where='papersize_id='.$id;
            $result=$this->getDetail('',$where);
      
            return $result;
           
            
            
        }
        function get_data_papername($id){
            $this->tableName='bur_papers';
            $where='paper_id='.$id;
            $result=$this->getDetail('',$where);
      
            return $result;
           
            
            
        }
        
        function get_papersize_format($id){
          $size=$this->get_data_papersize($id);
          $paper_size=$size['papersize_title'].' ';
               $paper_size.=$size['papersize_width'];
               $paper_size.=' x ';
               $paper_size.=$size['papersize_height'];
               $paper_size.='mm';
               return $paper_size;
           
            
            
        }
          function get_papername_format($id){
          $name=$this->get_data_papername($id);
          
              $paper_name=$name['paper_title'].' ';
             $paper_name.=$name['paper_colour'].' ';
             $paper_name.=$name['paper_size'].' gsm';
             return $paper_name;
            
            
        }
        
        function get_data($table_name,$field,$con_field,$con_id){
           
            $this->db->select($field);
            $this->db->where($con_field,$con_id);
            $query=$this->db->get($table_name);
           // echo $this->db->last_query();
            if($query->num_rows()>0){
               $res= $query->row_array();
              return $res[$field];
            }
            
        }
        function list_quotes_paper_data($table_name){
			if($table_name == 'bur_papercolours')
				$this->db->order_by('papercolour_order','DESC');
				
            $query=$this->db->get($table_name);
            if($query->num_rows()>0){
                return $query->result_array();
            }
            
        }
        
		function request_a_quote_get_all(){
		
		
		$this->setTableName('bur_quotes');
		return $this->getList();

		
		}
		function total_quotes($region = 0,$search = 'none') {
		$this->db->from($this->quotes_table);
	
		return $this->db->count_all_results();
	}
	
	function get_quotes($limit='false',$offset='false'){
    	if ($limit){
        	$limit = (int)$limit;
    		$offset = (int)$offset;
        }
		
		$this->db->order_by('quote_id','DESC');
      

		if ($limit){
            $query = $this->db->get($this->quotes_table,$limit,$offset);
        }else{
            $query = $this->db->get($this->quotes_table);
        }
        
		if($query->num_rows() > 0){
			return $query->result_array();
		}else{
			return FALSE;
		}
	}
	
	function get_quotes_detail($quote_id=''){
		
		$this->db->where('quote_id',$quote_id);
	 	$query = $this->db->get($this->quotes_table);
		if($query->num_rows() > 0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}

    function delete_quotes($quotes_id ){
        if(!empty($quotes_id)){
            $this->db->where('quote_id',$quotes_id);
            $this->db->delete($this->quotes_table);
            //echo $this->db->last_query();
            
        } else {
            return False;
            
        }
        
        
    }	
	
	function lookup($id)
	{
		
				//return $query = $this->db->query("SELECT * FROM ".$this->quotes_table." WHERE paper_size LIKE '".$id."%' ");
		$result = array();
		$this->load->database();
		$sql = "SELECT paper_id,concat(paper_title,' - ',paper_colour,' - ',paper_size,'gsm') as paper_vals FROM bur_papers WHERE paper_title LIKE '%".$id."%' ||  paper_colour LIKE '%".$id."%' || paper_size LIKE '%".$id."%' ";
		
		$items = $this->db->query($sql);
		if ($items->num_rows() > 0)
		{
					foreach ($items->result() as $item) {
			
					//array_push($result, array("id"=>$item->paper_id, "value" => $item->paper_title));
					array_push($result, array("id"=>$item->paper_id, "value" => $item->paper_vals));
					//array_push($result, array("id"=>$key, "value" => $val));
				
				if (count($result) > 11)
					break;
			}
		}
		else
		{
			array_push($result, array("id"=>'', "value" => "No records found"));
		}
		
		return ($result);
			
		
		
	}
	
	function lookup_size($id)
	{
		
				//return $query = $this->db->query("SELECT * FROM ".$this->quotes_table." WHERE paper_size LIKE '".$id."%' ");
		$result = array();
		$this->load->database();
		$sql = "SELECT papersize_id,concat(papersize_title,' - ',papersize_width,' X ',papersize_height,'mm') as paper_vals FROM `bur_papersizes` WHERE `papersize_title` like '%".$id."%' || `papersize_width` like '%".$id."%' || `papersize_height` like '%".$id."%'";
		//$sql = "SELECT paper_id,concat(paper_title,' - ',paper_colour,' - ',paper_size,'gsm') as paper_vals FROM bur_papers WHERE paper_title LIKE '%".$id."%' ||  paper_colour LIKE '%".$id."%' || paper_size LIKE '%".$id."%' ";
		
		$items = $this->db->query($sql);
		if ($items->num_rows() > 0)
		{
					foreach ($items->result() as $item) {
			
					//array_push($result, array("id"=>$item->paper_id, "value" => $item->paper_title));
					array_push($result, array("id"=>$item->papersize_id, "value" => $item->paper_vals));
					//array_push($result, array("id"=>$key, "value" => $val));
				
				if (count($result) > 11)
					break;
			}
		}
		else
		{
			array_push($result, array("id"=>'', "value" => "No records found"));
		}
		
		return ($result);
			
		
		
	}
	
}

?>